package com.excuse.excusebuilder.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.excuse.excusebuilder.R;
import com.excuse.excusebuilder.bean.MessageLeft;
import com.excuse.excusebuilder.bean.User;
import com.excuse.excusebuilder.commen.util.NetWorkUtil;
import com.excuse.excusebuilder.commen.util.ToastUtil;
import com.excuse.excusebuilder.viewholder.CommunityItemDetailViewholder;

import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by U-nookia on 2017/4/20.
 */

public class CommunityItemDetailAdapter extends RecyclerView.Adapter<CommunityItemDetailViewholder> {

    private List<MessageLeft> msgs;
    private Context context;

    public CommunityItemDetailAdapter(List<MessageLeft> msgs) {
        this.msgs = msgs;
    }

    @Override
    public CommunityItemDetailViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new CommunityItemDetailViewholder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_community_msg_left,parent,false));
    }

    @Override
    public void onBindViewHolder(CommunityItemDetailViewholder holder, final int position) {
        CircleImageView head = holder.getView(R.id.msg_left_head);
        TextView nameFrom = holder.getView(R.id.msg_left_name);
        TextView contentFrom = holder.getView(R.id.msg_left_content);
        TextView nameTo = holder.getView(R.id.msg_left_to_name);
        TextView contentTo = holder.getView(R.id.msg_left_to_content);
        TextView time = holder.getView(R.id.msg_left_time);
        RelativeLayout item = holder.getView(R.id.item_msg_left);
        Glide.with(context).load(msgs.get(position).getMessageFrom().getAvatarUrl()).into(head);
        nameFrom.setText(msgs.get(position).getMessageFrom().getUsername());
        contentFrom.setText(msgs.get(position).getContent());
        nameTo.setText(msgs.get(position).getMessageTo().getUsername());
        contentTo.setText(msgs.get(position).getContentTo());
        time.setText(msgs.get(position).getCreatedAt());
        item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickMsgLeft(msgs.get(position));
            }
        });
    }

    private void clickMsgLeft(final MessageLeft messageLeft) {
        new MaterialDialog.Builder(context)
                .content("回复 "+messageLeft.getMessageFrom().getUsername()+":")
                .inputType(InputType.TYPE_CLASS_TEXT)
                .input("输入回复内容", "", new MaterialDialog.InputCallback() {
                    @Override
                    public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
                        saveLeftMsgToBmob(messageLeft,input.toString());
                    }
                }).show();
    }

    private void saveLeftMsgToBmob(MessageLeft msgLeft,String input) {
        final MessageLeft msg = new MessageLeft();
        if (input.equals("")) return;
        if (BmobUser.getCurrentUser()==null) {
            ToastUtil.makeShortToast(context,"请先登录");
            return;
        }
        if (!NetWorkUtil.netWorkConnected){
            ToastUtil.makeShortToast(context,"请检查网络");
            return;
        }
        msg.setContent(input);
        msg.setPost(msgLeft.getPost());
        msg.setContentTo(msgLeft.getContent());
        msg.setMessageFrom(BmobUser.getCurrentUser(User.class));
        msg.setMessageTo(msgLeft.getMessageFrom());
        msg.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e==null){
                    msgs.add(msg);
                    notifyDataSetChanged();
                }else {
                    ToastUtil.makeShortToast(context,"回复出错，请重试");
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return msgs.size();
    }
}
